package com.app.newiptv.adaper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.LatelyPresenter;
import com.app.newiptv.utils.FocusedViewUtils;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;

/**
 * Created by Shinelon on 2017/9/15.
 */

public class LatelyAdapter extends RecyclerView.Adapter<LatelyAdapter.MainRecycleViewHolder> {
    private LatelyPresenter presenter;
    private int count;
    private View focusview;
    private RecyclerView parentRecycle;
    public View currentView;
    private Context mContext;

    //private Handler handler = new Handler(){
    //    @Override
    //    public void handleMessage(Message msg) {
    //        super.handleMessage(msg);
    //    }
    //};
    public LatelyAdapter(LatelyPresenter presenter, Context mContext) {
        this.presenter = presenter;
        this.mContext = mContext;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setFocusView(View view) {
        this.focusview = view;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lately_view, null, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        if (parentRecycle == null) {
            parentRecycle = (RecyclerView) parent;
            parentRecycle.addOnScrollListener(new MovieWorldScrollListener());
        }
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindMainChanelView(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements LatelyPresenter.MainViewHolder {

        private int position;
        private ImageView lately_image;
        private TextView lately_title;
        private ImageView time_r_index;
        private TextView timer_text;
        private RelativeLayout lately_view;
        private RelativeLayout item_layout;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            lately_image = (ImageView) itemView.findViewById(R.id.lately_image);
            lately_title = (TextView) itemView.findViewById(R.id.lately_title);
            time_r_index = (ImageView) itemView.findViewById(R.id.time_r_index);
            timer_text = (TextView) itemView.findViewById(R.id.timer_text);
            lately_view = (RelativeLayout) itemView.findViewById(R.id.lately_view);
            item_layout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
            lately_title.setTypeface(MyApplication.face);
            timer_text.setTypeface(MyApplication.face);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        currentView = item_layout;
                        boolean scroll = FocusedViewUtils.arrowScroll(view);
                        if (!scroll) {
                            FocusedViewUtils.setFocusedView(focusview, item_layout, 20, 20, 25, 25);
                            //lately_title.animate().scaleX(1.2f);
                            //lately_title.animate().scaleY(1.5f);
                        }
                    } else {
                        FocusedViewUtils.setBlurView(focusview, item_layout);
                        focusview.setVisibility(View.INVISIBLE);
                        //lately_title.animate().scaleX(1.0f);
                        //lately_title.animate().scaleY(1.0f);
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.latelyPosition(position);
                }
            });
        }

        private void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void setlatelyTitle(String title) {
            lately_title.setText(title);
        }

        @Override
        public void setlatelyImg(String image) {
            Glide.with(mContext).load(Paths.HOSTPATH + image).into(lately_image);
        }

        @Override
        public void setlateTimer(String timer) {
            if (timer == null || timer.isEmpty()) {
                time_r_index.setVisibility(View.INVISIBLE);
                timer_text.setVisibility(View.INVISIBLE);
            } else {
                time_r_index.setVisibility(View.VISIBLE);
                timer_text.setVisibility(View.VISIBLE);
                timer_text.setText(timer);
            }
        }
    }

    class MovieWorldScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                FocusedViewUtils.setFocusedView(focusview, currentView, 20, 20, 25, 25);
            }
        }
    }
}
